<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="报修编码" prop="assetCode">
        <el-select v-model="queryParams.assetCode" filterable placeholder="请选择报修编码">
            <el-option
              v-for="item in assetCodeList"
              :key="item.code"
              :label="item.title+'('+item.code+')'"
              :value="item.code+''">
            </el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="报修人" prop="repairBy">
        <el-input
          v-model="queryParams.repairBy"
          placeholder="请输入报修人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="报修状态" prop="status">
          <el-select v-model="queryParams.status" clearable filterable placeholder="请选择报修状态">
            <el-option
              v-for="dict in dict.type.bxmanage_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
              size="small">
            </el-option>
          </el-select>
      </el-form-item>
      <!-- <el-form-item label="报修时间" prop="repairTime">
          <el-date-picker
            v-model="queryParams.repairTime"
            type="datetime"
            placeholder="选择报修时间">
          </el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="repaireList" @selection-change="handleSelectionChange">
      <el-table-column label="报修编码" align="center" prop="assetCode"></el-table-column>
      <el-table-column label="报修原因" align="center" prop="repair" />
      <el-table-column label="报修人" align="center" prop="repairBy" />
      <el-table-column label="报修时间" align="center" prop="repairTime" />
      <el-table-column label="报修状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bxmanage_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改班级对话框 -->
    <el-dialog  :close-on-click-modal="false" :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <div v-if="isType==1">
          <el-form-item label="报修编码" prop="assetCode">
          <el-select v-model="form.assetCode" filterable placeholder="请选择报修编码">
            <el-option
              v-for="item in assetCodeList"
              :key="item.code"
              :label="item.title+'('+item.code+')'"
              :value="item.code+''">
            </el-option>
        </el-select>
        </el-form-item>
       <el-form-item label="报修原因" prop="repair">
        <el-input type="textarea" v-model="form.repair"  :rows="4" placeholder="请输入报修原因">
        </el-input>
       </el-form-item>
        <!-- <el-form-item label="报修人" prop="repairBy">
          <el-select v-model="form.repairBy" filterable placeholder="请选择报修人">
            <el-option
              v-for="item in teacherList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="报修时间" prop="repairTime">
          <el-date-picker
            v-model="form.repairTime"
            type="datetime"
            placeholder="选择报修时间">
          </el-date-picker>
        </el-form-item>
        </div>
        <div v-else>
          <el-form-item label="报修状态">
          <el-radio-group v-model="form.status" @input="$forceUpdate()">
            <el-radio :label="0">待处理</el-radio>
            <el-radio :label="1">已处理</el-radio>
            <!-- 0待处理1已处理', -->
          </el-radio-group>
        </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {  delClazz} from "@/api/base/clazz";
import { repairList,addRepair,editRepair,getbxSelectList } from '@/api/amgSystem/bxmanage.js'
import {  selectTeacher } from '@/api/base/teacher'
import { selectRoom} from "@/api/base/room";
import { getToken } from "@/utils/auth";

export default {
  name: "Clazz",
  dicts: ['year_type','bxmanage_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报修表格数据
      repaireList: [],
      // 弹出层标题
      title: "",
      isType:1,//1.新增2.修改
      // 是否显示弹出层
      open: false,
      //起始年
      yearList:[],
      // 专业表格数据
      specialtyList: [],
      // 用户数据
      teacherList: [],
      // 教室数据
      roomList: [],
      // 状态字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        assetCode: [
          { required: true, message: "报修编码不能为空", trigger: "blur" }
        ],
        repair: [
          { required: true, message: "报修原因不能为空", trigger: "blur" }
        ],
        repairTime: [
          { required: true, message: "报修时间不能为空", trigger: "blur" }
        ],
      },
      assetCodeList:[]
    };
  },
  created() {
    this.getList();
    this.getbxSelectList()
    this.getDicts("common_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    getbxSelectList(){
      this.loading = true;
      getbxSelectList({schoolCode:this.$store.getters.schoolCode}).then(res =>{
        this.assetCodeList = res.data;
        this.loading = false;
      })
    },
    dealTime(time){
        // 将时间字符串转换为日期对象
        var date = new Date(time);

        // 获取年、月、日、小时、分钟和秒
        var year = date.getFullYear();
        var month = ("0" + (date.getMonth() + 1)).slice(-2);
        var day = ("0" + date.getDate()).slice(-2);
        var hours = ("0" + date.getHours()).slice(-2);
        var minutes = ("0" + date.getMinutes()).slice(-2);
        var seconds = ("0" + date.getSeconds()).slice(-2);

        // 构建以"-"分割的时间格式
        var formattedTime = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
        return formattedTime
    },
    /** 查询报修列表 */
    getList() {
      this.loading = true;
      repairList(this.queryParams).then(response => {
        this.repaireList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        assetCode:'',
        status:null,

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.isType = 1
      this.title = "添加报修资产";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.form.id = row.id
      this.open = true;
      this.form.status = row.status
      this.isType = 2
      this.title = "修改报修资产";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.isType == 2) {
            editRepair(this.form).then(response => {
              if(response.code==200){
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }else{
                this.msgError(response.msg);
              }

            });
          } else {
            this.form.repairTime = this.dealTime(this.form.repairTime)
            this.form.repairBy = this.$store.getters.roles[0]
            addRepair(this.form).then(response => {
              if(response.code==200){
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }else{
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除选中班级?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delClazz(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    }
  }
};
</script>
<style lang="scss" scoped>
.el-dialog__body .el-select,.el-dialog__body .el-date-editor{
  width: 100%;
}
</style>
